var loginname;
var loginkey;
var submit_info='<div class="form-group-col-2">'+
					'<div class="form-label"></div>'+
					'<div class="form-cont">'+
					   '<input type="button" value="提交" onclick="save_timeout_setting();"  class="btn btn-info" />'+
					'</div>'+
				'</div>';
$(function(){
	loginname = $.session.get("username");
	loginkey = $.session.get("user_secretkey");
	$("#from_timeout").html("");
	timeout_setting_info();
})
function timeout_setting_info(){
	var infos="";
	$.ajax({
		url: "../../../../service",
		cache: false,
		async: false,
		data: {
			proc: "xmy_zyr.timeout_setting_info",
			para: "['" + loginname + "','" + loginkey + "']"
		},
		dataType: "json",
		success: function(data) {
			//console.log(JSON.stringify(data));
			for(var i=0;i<data.length;i++){
				infos='<div class="form-group-col-2 timeout_class">'+
							'<div class="form-label"><span class="timeout_type" id="out_'+data[i].ID+'">'+data[i].TYPENAME+'</span>超时：</div>'+
							'<div class="form-cont">'+
							   '<input type="text" value="'+data[i].SETTIME+'" class="form-control form-boxed width30 outtime_hour"  placeholder="设置超时时间分钟数" />提醒<input type="text" value="'+data[i].REMINDTIME+'" class="form-control form-boxed width30 outtime_remind"  placeholder="提醒时间分钟数" />'+
							   '<input type="checkbox"  '+(data[i].ISOPEN=='1'?' checked':'')+'>'+
						   '</div>'+
					   '</div>';
				$("#from_timeout").append(infos);	   
			}
			$(".form-content form").append(submit_info);
		}
	});
	
}

function save_timeout_setting(){
	var str_timeout="";
	var counts=0;
	$(".timeout_class").each(function(){
		counts++;
		var timeout_type=$(this).find(".timeout_type").html();//超时环节
		var timeout_id=$(this).find(".timeout_type").attr("id");//超时id
		timeout_id=timeout_id.split("out_")[1];
		var outtime_hour=$(this).find(".form-cont .outtime_hour").val();//设置超时时间小时数
		var outtime_remind=$(this).find(".form-cont .outtime_remind").val();//提醒时间小时数
		var checked_open=$(this).find(".bootstrap-switch").hasClass("bootstrap-switch-on");//是否打开
		checked_open=(checked_open)?1:0;
		str_timeout+=timeout_id+':'+outtime_hour+","+outtime_remind+"/"+checked_open+"&";
	})
	//console.log(str_timeout+"==str_timeout--=="+counts);
	top.layer.confirm('是否确定提交信息？',{icon: 3,title:'提示信息'},function(index){	
		var index = top.layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
		setTimeout(function(){
			$.ajax({
				url: "../../../../service",
				cache: false,
				async: false,
				data: {
					proc: "xmy_zyr.save_timeout_setting",
					para: "['" + loginname + "','" + loginkey + "','" + str_timeout + "']"
				},
				dataType: "json",
				success: function(data) {
					console.log(JSON.stringify(data));  						
					if(data[0].TAG=='0'){
						top.layer.alert(""+data[0].MSG+"",function(index){
						   top.location.href="login.html";
						   top.layer.closeAll();
						   layer.close(index);	
					    });    
					}else if(data[0].msg=='读取接口失败,参数错误'||data.msg=='读取接口失败,参数错误'){
						top.layer.closeAll();
						top.layer.msg("修改失败，请检查您的网络环境!");		
						console.log("读取接口失败,参数错误");
					}else if(data[0].TAG == 2){
						top.layer.msg(""+data[0].MSG+"");
						window.opener.location.reload();
					}else{
						top.layer.closeAll();							
						top.layer.msg(""+data[0].MSG+"");
						frea_info();
						window.location.href="timeout_setting.html";
					}
				}
			}); 					
		},1000);
	})	
	
}
/*刷新index数据*/
function frea_info(){
	var href_url= window.location.host;  
	var script = document.createElement("script");
	script.src = "http://"+href_url+"/admin/js/index.js";
	document.body.appendChild(script);
}